(function() {
	
	var factory = function(exports) {

		var $ = jQuery; // if using module loader(Require.js/Sea.js).

		var langs = {
			"zh-cn": {
				toolbar: {
					color: "字体颜色"
				},
				dialog: {
					color: {
						title: "添加字体颜色",
						colorValue: "颜色值",
						colorEmpty: "请输入颜色值",
						defaultColor: "#00a1ea"
					}
				}
			},
			"zh-tw":{
				toolbar: {
					color: "字体颜色"
				},
				dialog: {
					color: {
						title: "添加字体颜色",
						colorValue: "颜色值",
						colorEmpty: "请输入颜色值",
						defaultColor: "#00a1ea"
					}
				}
			},
			"en":{
				toolbar: {
					color: "字体颜色"
				},
				dialog: {
					color: {
						title: "添加字体颜色",
						colorValue: "颜色值",
						colorEmpty: "请输入颜色值",
						defaultColor: "#00a1ea"
					}
				}
			}

		};

		var pluginName = "color-dialog";

		exports.fn.colorDialog = function() {

			var _this = this;
			var cm = this.cm;
			var editor = this.editor;
			var settings = this.settings;
			var path = settings.path + "../plugins/" + pluginName + "/";
			var selection   = cm.getSelection();
			var cursor = cm.getCursor();
			var classPrefix = this.classPrefix;
			var dialogName = classPrefix + pluginName,
				dialog;

			$.extend(true, this.lang, langs[this.lang.name]); // l18n
			this.setToolbar();
			
			var lang        = this.lang;
			var colorLang  = lang.dialog.color;

			cm.focus();

			if(editor.find("." + dialogName).length > 0) {
				dialog = editor.find("." + dialogName);
				dialog.find("[data-color]").val(colorLang.defaultColor);

				this.dialogShowMask(dialog);
				this.dialogLockScreen();
				dialog.show();
			} else {
				var dialogHTML = "<div class=\"" + classPrefix + "form\">" +
					"<label>" + colorLang.colorValue + "</label>" +
					"<input type=\"text\" value=\""+colorLang.defaultColor+"\" data-color />" +
					"<br/>" +
					"</div>";

				dialog = this.createDialog({
					title: colorLang.title,
					width: 380,
					height: 160,
					content: dialogHTML,
					mask: settings.dialogShowMask,
					drag: settings.dialogDraggable,
					lockScreen: settings.dialogLockScreen,
					maskStyle: {
						opacity: settings.dialogMaskOpacity,
						backgroundColor: settings.dialogMaskBgColor
					},
					buttons: {
						enter: [lang.buttons.enter, function() {
							var colorValue = this.find("[data-color]").val();

							if(colorValue === "#") {
								alert(colorLang.colorEmpty);
								return false;
							}

							var str1 = "<span style=\"color:" + colorValue + ";\">";
							var str2 = "</span>"

							$(".fa-adn").css("color",colorValue);
							
							cm.replaceSelection(str1+selection+str2);
							cm.setSelection({line:cursor.line,ch:str1.length+cursor.ch-selection.length},{line:cursor.line,ch:str1.length+cursor.ch});
							

							this.hide().lockScreen(false).hideMask();

							return false;
						}],

						cancel: [lang.buttons.cancel, function() {
							this.hide().lockScreen(false).hideMask();

							return false;
						}]
					}
				});
			}
		};

	};

	// CommonJS/Node.js
	if(typeof require === "function" && typeof exports === "object" && typeof module === "object") {
		module.exports = factory;
	} else if(typeof define === "function") // AMD/CMD/Sea.js
	{
		if(define.amd) { // for Require.js

			define(["editormd"], function(editormd) {
				factory(editormd);
			});

		} else { // for Sea.js
			define(function(require) {
				var editormd = require("./../../editormd");
				factory(editormd);
			});
		}
	} else {
		factory(window.editormd);
	}

})();